﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    /// <summary>
    /// 充值操作
    /// </summary>
    public static partial class huifucontroller
    {
        /// <summary>
        /// 用户充值界面获取数据
        /// </summary>
        [json]
        public static void huifu_chongzhi(pagebase pb)
        {
            string zongjine = "";
            string dongjieyue = "";
            // 账户余额
            string money_balance =
                huifu_chaxun_yue(core_user.userID, out zongjine, out dongjieyue, false);

            // 账户总额 = 账户余额+ 投资总额(冻结)
            var money_amount = zongjine;
            //冻结金额
            var money_congeal = dongjieyue;

            BaseJson json = pb.JSON<BaseJson>();

            //返回json数据
            json.data = new
            {
                money_balance = money_balance,
                money_amount = money_amount,
                money_congeal = money_congeal,
                gryh = supermodel //个人银行
                            .create("SysUser_BankType")
                            .where("typeid=1 and Enable=1")
                            .list(),
                gsyh = supermodel//公司银行
                            .create("SysUser_BankType")
                            .where("typeid=2 and Enable=1")
                            .list(),
                BankTypeid = supermodel//判断用户是否绑定默认银行卡
                            .create("SysUser_Bank")
                            .select("BankTypeid")
                            .where("user_id='" + core_user.userID + "' and UseDefault=0")
                            .one<string>()
            };           
        }

        /// <summary>
        /// 商户充值，这个应该是后台调用。
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_chongzhi_shanghu_submit(pagebase pb)
        {
            var jine = pb.v_dec("jine");
            var money = pb.v_dec("money");
            if (jine <= 0) throw new Exception("金额不能小于0");

            NetSave objsend = new NetSave();
            objsend.MerCustId = HuiFuArg.MerCustId;
            objsend.UsrCustId = HuiFuArg.MerCustId;

            objsend.GateBusiId = "";
            objsend.OpenBankId = "";
            //充钱必须是.00
            objsend.TransAmt = jine.ToString("#0.00");//充值的金额数

            //跟普通用户一致
            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_chongzhi_submit_returnbg";
            objsend.RetUrl = HuiFuArg.LocalUrl + "huifu/huifu_chongzhi_submit_return";

            //插入到充值流水表中
            supermodel Msavemoney = supermodel.create("Msavemoney");
            Msavemoney["User_Id"] = HuiFuArg.MerCustId;
            Msavemoney["SetupDate"] = DateTime.Now;
            Msavemoney["LogType"] = 0;
            Msavemoney["OrdId"] = objsend.OrdId + "";
            Msavemoney["OrdDate"] = objsend.OrdDate + "";
            Msavemoney["TransAmt"] = objsend.TransAmt;
            Msavemoney["Type"] = pb.v_s("Type").tostr();
            //lxw 20141108 添加.s
            Msavemoney["maxid"] = pb.v_s("maxid");
            var ret = Msavemoney.saveorupdate();
            //插入成功后，把流水的ID传递到实体中
            objsend.MerPriv = ret.identity + "";
            //提交到汇付的界面
            core_huifu.Submit(objsend);
        }


        /// <summary>
        /// 提交到汇付界面充值，submit
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_chongzhi_submit(pagebase pb)
        {
            //提交充值信息
            NetSave objsend = new NetSave();
         
            supermodel userinfo = core_user.getUserInfo();
            objsend.UsrCustId = userinfo["UsrCustId"] + "";

           

            objsend.GateBusiId = pb.v_s("bankString").tostr().Split('_')[0].tostr();
            objsend.OpenBankId = pb.v_s("bankString").tostr().Split('_')[1].tostr();
            //网关的细分业务类型
            //B2C--B2C 网银支付 B2B--B2B 网银支付 QP--快捷支付

            //D--借记，储蓄卡 C--贷记 ，信用卡
            objsend.DcFlag = "D";
            
            //充钱必须是.00
            objsend.TransAmt = pb.v_s("jine").todecimal().ToString("#0.00");//充值的金额数
            //this.CancelView();

            //插入到充值流水表中
            supermodel Msavemoney = supermodel.create("Msavemoney");
            Msavemoney["User_Id"] = core_user.userID;
            Msavemoney["SetupDate"] = DateTime.Now;
            Msavemoney["LogType"] = 0;
            Msavemoney["OrdId"] = objsend.OrdId + "";
            Msavemoney["OrdDate"] = objsend.OrdDate + "";
            Msavemoney["TransAmt"] = objsend.TransAmt;

            //lxw 20141108 添加.s  
            //lxw 20150724 获取外部传递过来的最大编号，这个地方最好也跟类别绑定一下。
            Msavemoney["maxid"] = pb.v_s("maxid");
            //lxw 20141108 添加.e
            //data["GateBusiId"] = DateTime.Now;
            //data["OpenBankId"] = DateTime.Now;
            //data["DcFlag"] = DateTime.Now;

            //data["TrxId"] = DateTime.Now;
            var ret = Msavemoney.saveorupdate();

            //插入成功后，把流水的ID传递到实体中
            objsend.MerPriv = ret.identity + "";

            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_chongzhi_submit_returnbg";
            objsend.RetUrl = HuiFuArg.LocalUrl + "huifu/huifu_chongzhi_submit_return";


            core_huifu.Submit(objsend);
        }

        /// <summary>
        /// 充值成功调用的方法
        /// </summary>
        /// <param name="objrev"></param>
        static void huifu_chongzhi_submit_return_fun(RetNetSave objrev)
        {
            supermodel Msavemoney = supermodel.create("Msavemoney");
            Msavemoney["GateBusiId"] = objrev.GateBusiId + "";
            Msavemoney["OpenBankId"] = objrev.GateBankId + "";
            //data["DcFlag"] = DateTime.Now;
            Msavemoney["Msavemoneyid"] = objrev.MerPriv + "";
            Msavemoney["TrxId"] = objrev.TrxId + "";
            Msavemoney["LogType"] = "1";
            Msavemoney["UsrCustId"] = objrev.UsrCustId + "";
            Msavemoney["OrdDate"] = objrev.OrdDate + "";

            Msavemoney.saveorupdate();
        }

        /// <summary>
        /// 充值成功显示页面
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_chongzhi_submit_return(pagebase pb)
        {
            RetNetSave objrev = new RetNetSave();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "TrxId", objrev, false);
            
            //代表成功！
            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                //充值成功。判断这条流水是否已经被处理，已经被处理，不能再重复处理
                //objsend.MerPriv这个就是充值流水的ID
                //增加用户的资金
                //设置资金的表示
                huifu_chongzhi_submit_return_fun(objrev);
                
                //发送短信
                supermodel userinfo = core_user.getUserInfoByUsrCustId(objrev.UsrCustId);

                if (userinfo.Count > 0)
                {
                    core_sms.SendMsg(userinfo["MOBIL_NO"].tostr(), "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功充值" + objrev.TransAmt.tostr() + "元整。若非本人操作，请您联系致电客服。400-100-0616");
                    //发送站内信
                    core_user.sendMessage(userinfo["user_id"].tostr(), "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功充值" + objrev.TransAmt.tostr() + "元整。若非本人操作，请您联系致电客服。400-100-0616");
                }

                //JSHelper.Redirect("/PUser/message.html?status=1&msg=NetSave");
            }
            else
            {
                //JSHelper.Redirect("/PUser/message.html?status=0&msg=NetSave");
            }
        }

        /// <summary>
        /// 充值成功显示页面 后台返回
        /// </summary>
        public static void huifu_chongzhi_submit_returnbg(pagebase pb)
        {
            RetNetSave objrev = new RetNetSave();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "TrxId", objrev, true);
          

            //代表成功！
            if (objrev.RespCode == core_huifu.HUIFU_SUCCESS)
            {
                //充值成功。判断这条流水是否已经被处理，已经被处理，不能再重复处理
                //增加用户的资金
                //设置资金的表示
                huifu_chongzhi_submit_return_fun(objrev);
            }
        }
    }
}